// components/dialog-sample/dialogSample.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
      observer: function(show){
        if (show) {
        } else {
          clearInterval(this.data.intervalId)
          this.setData({
            showText: [],
            intervalId: ''
          })
        }
      }
    },
    sceneId: {
      type: String,
      observer: function(newVal, oldVal){
        this.getDialogs()
      }
    },
    trainSceneId: {
      type: String,
      value: ''
    },
    recordType: {
      type: String,
      value: ''
    },
    onlyCustomer: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    CDNPATH: app.globalData.CDNPATH,
    imgResUrl: '',
    list: [],
    showText: [],
    scrollTop: 0,
    intervalId: '',
  },

  ready: function () {
    console.log(this.data.CDNPATH)
    this.setData({
      imgResUrl: app.globalData.userInfo.imgResUrl
    })
    // if (this.data.sceneId) {
    //   this.getDialogs()
    // }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadWord: function () {
      const self = this
      var index = 0
      var i = 0;
      this.setData({
        [`showText[${index}]`]: {...this.data.list[index]}
      })
      const intervalId = setInterval(() => {
        const { word } = this.data.list[index]
        const text = word.substring(0, ++i);
        this.setData({
          [`showText[${index}].text`]: text
        });
        const query = this.createSelectorQuery()
        query.select('#dialog-wrap').boundingClientRect()
        query.exec((res) => {
          res[0] && this.setData({
            scrollTop: res[0].height
          })
        })
        if (i === word.length) {
          //   console.log("定时器结束！");
          if ((index + 1) === this.data.list.length) {
            clearInterval(intervalId);
          } else {
            index++
            this.setData({
              [`showText[${index}]`]: {...this.data.list[index]}
            });
            i = 0
          }
        }
      }, 200)
      console.log(intervalId)
      this.setData({
        intervalId
      })
    },
    getDialogs: function () {
      const {sceneId,trainSceneId,levelId,recordType} = this.data
      app.api.post({
        // 根据场景id查询场景对话信息
        url: 'scene_manage_get_nodes',
        data: {
          trainSceneId:trainSceneId!=='undefined'?trainSceneId:'',
          levelId: levelId!=='undefined'?levelId:'',
          sceneId,
          recordType,
        }
      }).then(res => {
        let list = []
        if (this.data.onlyCustomer) {
          res.sceneNodeInfos.map(({replyScript, standardScript}) => {
            list.push({role: 'customer', word: standardScript})
          })
        } else {
          if (res.sceneType === 'knowledge_question') {
            // 知识问答,机器人先说
            res.sceneNodeInfos.map(({replyScript, standardScript}) => {
              list.push({role: 'robot', word: replyScript}, {role: 'customer', word: standardScript})
            })
          } else {
            // 流程话术,客户先说
            res.sceneNodeInfos.map(({replyScript, standardScript}) => {
              list.push({role: 'customer', word: standardScript}, {role: 'robot', word: replyScript})
            })
          }
        }
        this.setData({
          list: list,
        })
        this.loadWord()
      })
    },
    close: function () {
      this.triggerEvent('close', {}, {})
    }
  }
})
